// pages/visitor/home/home.js
var api = require('../../../utils/httpUtil.js');
const app = getApp();
Component({
  options: {
    addGlobalClass: true,
  },
  data: {
    imgUrl: api.imgUrl,
    baseUrl: api.baseUrl,
    iconList: [{
      id: 1,
      icon: 'friendfill',
      color: 'blue',
      badge: 0,
      name: '访客申请',
      url: '/pages/visitor/apply/apply'
    }, {
      id: 2,
      icon: 'locationfill',
      color: 'cyan',
      badge: 0,
      name: '导航',
      url: '/pages/navigate/navigate'
    }, {
      id: 3,
      icon: 'noticefill',
      color: 'yellow',
      badge: 0,
      name: '今日拜访',
      url: '/pages/visitor/center/center'
    }, {
      id: 4,
      icon: 'peoplefill',
      color: 'red',
      badge: 0,
      name: '会员注册',
      url: '/pages/member/register/register'
    }, {
      id: 5,
      icon: 'countdownfill',
      color: 'green',
      badge: 0,
      name: '环境监测',
      url: '/pages/env-monitor/env-monitor'
    }],
    newsList: [],
    gridCol: 4,
    skin: false,
    cardCur: 0,
    slideShow: [],
    houseImages: [],
    queryData: {}
  },
  lifetimes: {
    // 组件生命周期函数，在组件布局完成后执行，此时可以获取节点信息
    ready: function () {
      // 在组件布局完成后执行的操作
      if (app.globalData.userType == "member") {
        var list = this.data.iconList;
        this.data.iconList[3].name = "会员中心";
        this.data.iconList[3].url = "/pages/member/index/index";
        this.setData({
          iconList: list
        })
      }
      this.loadAnnounce();
      this.loadAdv();
      this.loadLetter();
    }
  },
  methods: {
    // 点击事件处理函数，用于修改组件数据
    visitListClick: function () {
      wx.navigateTo({
        url: '/pages/visitor/center/center',
      })
    },
    recallClick: function () {
      wx.navigateTo({
        url: '/pages/more/develop',
      })
    },
    newsClick: function (e) {
      var id = e.currentTarget.dataset.id;
      app.globalData.news = this.data.newsList[id];
      wx.navigateTo({
        url: '/pages/news/news'
      })
    },
    letterClick: function(e){
      var id = e.currentTarget.dataset.id;
      app.globalData.newsletter = this.data.houseImages[id];
      wx.navigateTo({
        url: '/pages/newsletter/newsletter'
      })
    },
    //获取公司资讯
    loadAnnounce: function (e) {
      api.httpRequest(api.announceAPI, this.data.queryData)
        .then(res => {
          if (res.code == 200) {
            this.setData({
              newsList: res.data
            })
          } else {
            wx.showToast({
              title: res.msg,
              duration: 2000,
              mask: true,
              icon: 'none'
            })
          }

        })
        .catch(error => {
          wx.showToast({
            title: error.message,
            duration: 1000,
            mask: true,
            icon: 'none'
          })
        });
    },

    //获取广告
    loadAdv: function (e) {
      api.httpRequest(api.advAPI, this.data.queryData)
        .then(res => {
          if (res.code == 200) {
            this.setData({
              slideShow: res.data.slideshow
            })
          } else {
            wx.showToast({
              title: res.msg,
              duration: 2000,
              mask: true,
              icon: 'none'
            })
          }

        })
        .catch(error => {
          wx.showToast({
            title: error.message,
            duration: 1000,
            mask: true,
            icon: 'none'
          })
        });
    },

    //获取活动
    loadLetter: function (e) {
      api.httpRequest(api.newsletterAPI, this.data.queryData)
        .then(res => {
          if (res.code == 200) {
            this.setData({
              houseImages: res.data
            })
          } else {
            wx.showToast({
              title: res.msg,
              duration: 2000,
              mask: true,
              icon: 'none'
            })
          }

        })
        .catch(error => {
          wx.showToast({
            title: error.message,
            duration: 1000,
            mask: true,
            icon: 'none'
          })
        });
    },
  }

})